<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库" prop="name">
        <el-select v-model="queryParams.depotId" clearable  placeholder="请选择仓库">
           <el-option
             v-for="item in depotAllList"
             :key="item.id"
             :label="item.name"
             :value="item.id">
           </el-option>
         </el-select>
      </el-form-item>
      <el-form-item label="条码" prop="materialParam">
        <el-input v-model="queryParams.materialParam" placeholder="条码/名称/规格/型号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>
    </el-row>
    <el-table v-loading="loading" max-height="620px" :data="stockWarningCountList" @selection-change="handleSelectionChange"
    :summary-method="getSummaries"
       show-summary
    >
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="仓库" align="center" prop="depotName" />
      <el-table-column label="条码" align="center" prop="barCode" />
      <el-table-column label="名称" align="center" prop="mname" />
      <el-table-column label="规格" align="center" prop="mstandard" />
      <el-table-column label="型号" align="center" prop="mmodel" />
      <el-table-column label="扩展信息" align="center" prop="materialOther" />
<el-table-column label="单位" align="center" prop="materialUnit" />
<el-table-column label="库存" align="center" prop="currentNumber" />
<el-table-column label="最低安全库存" align="center" prop="lowSafeStock" />
<el-table-column label="最高安全库存" align="center" prop="highSafeStock" />
<el-table-column label="建议入库量" align="center" prop="lowCritical" />
<el-table-column label="建议出库量" align="center" prop="highCritical" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 50, 100, 200,500, 1000,2000]"
      @pagination="getList"
    />


  </div>
</template>

<script>
 import { findStockWarningCount}  from "@/api/bill/retail";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
   import { materialcategorytreeselect } from "@/api/material/materialcategory";
    import { listDepotByCurrentUserAll } from "@/api/basicsystem/depot";
    import {
      getListWithStock
    } from "@/api/material/material";
	import * as math from 'mathjs'
    export default {
  name: "materialstock",
   components: { Treeselect },
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存预警数据
      stockWarningCountList: [],
      depotAllList:[],//仓库
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      depotIds:[],//被选中仓库ID
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zeroStock:"0",
        materialParam:'',
      depotIds:null//被选中仓库ID
      },
      currentStock:"",//总库存
      currentStockPrice:"",//总库存金额
      materialcategoryOptions:[],//类别
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicUnit: [
          { required: true, message: "基本单位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
     //this.materialcategorytreeselect();
     this.listAllDepot();
    this.getList();
  },
  methods: {

    getSummaries(param) {
                   const { columns, data } = param;
                   const sums = [];
                   columns.forEach((column, index) => {
                     if (index === 0) {
                       sums[index] = '合计';
                       return;
                     }
                     if (index === 1) {
                       sums[index] = '-';
                       return;
                     }
                     if (index === 2) {
                       sums[index] = '-';
                       return;
                     }
 if (index === 3) {
                       sums[index] = '-';
                       return;
                     }
 if (index === 4) {
                       sums[index] = '-';
                       return;
                     }
                     if (index === 5) {
                                           sums[index] = '-';
                                           return;
                                         }
                     if (index === 6) {
                       sums[index] = '-';
                       return;
                     }
if (index ===7) {
                       sums[index] = '-';
                       return;
                     }
                     const values = data.map(item => Number(item[column.property]));
                     if (!values.every(value => isNaN(value))) {
                       sums[index] = values.reduce((prev, curr) => {
						   const value = Number(curr);
							if (!isNaN(value)) {
							  return math.add(math.bignumber(prev),math.bignumber(curr));
							} else {
							  return math.bignumber(prev);
							}
                       }, 0);
                       sums[index] += '';//可以写单位
                     } else {
                       sums[index] = '0';
                     }
                   });

                   return sums;
                 },
    /** 查询所产仓库 */
    listAllDepot() {


      listDepotByCurrentUserAll(this.queryParams).then(response => {
       /* this.materialList = response.rows;
        this.total = response.total;
        this.loading = false; */
         this.depotAllList=response.list;

      });
    },
    /** 查询类型下拉树结构 */
    materialcategorytreeselect() {
      materialcategorytreeselect().then(response => {
        this.materialcategoryOptions = response.data;
        console.log( this.materialcategoryOptions)
      });
    },
    /** 查询库存列表 */
    getList() {
      this.loading = true;

      console.log(this.queryParams.depotId)
      findStockWarningCount(this.queryParams).then(response => {
        this.stockWarningCountList = response.data.rows;
        this.total = response.data.total;
        this.currentStock= response.currentStock;
         this.currentStockPrice= response.currentStockPrice;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaterialunit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计量单位";
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/bill/retail/stockWarningExport', {
          ...this.queryParams
        }, `库存预警_${this.parseTime(new Date(),'{y}-{m}-{d}')}.xlsx`)
    }
  }
};
</script>
